package org.adream.account.rest.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.adream.account.entity.SchoolEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.rest.api.model.ReBindDirectorForm;
import org.adream.account.rest.api.model.SchoolSearchForm;
import org.adream.account.service.ADreamServiceException;
import org.adream.account.service.SchoolService;
import org.adream.account.util.UserUtil;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

/**
 *	学校 
 *
 */
@Controller
@RequestMapping("/school")
public class SchoolApiController {
	
	@Autowired
	private SchoolService schoolService;
	
	private final static Logger logger = org.slf4j.LoggerFactory.getLogger(SchoolApiController.class);
	
	/**
	 * restful 有了接口  addSchoolData
	 * @param school
	 * @return
	 */
	@RequestMapping(value = "", produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<String> addSchool(@RequestBody SchoolEntity school,HttpServletRequest request) {
		if(school == null || StringUtils.isEmpty(school.getSname())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,请求参数失败", null);
		}
		return schoolService.addSchool(school);
	}
	
	/**
	 * 删
	 * @param sid
	 * @return
	 */
	@RequestMapping(value = "/{sid}", produces = "application/json;charset=UTF-8",method = RequestMethod.DELETE)
	@ResponseBody
	public ResultModel<String> deleteSchool(@PathVariable("sid") String sid) {
		if(StringUtils.isEmpty(sid)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,请求参数有误", null);
		}
		return schoolService.deleteSchool(sid);
	}
	
	/**
	 * 更新
	 * @param sid
	 * @param schoolEntity
	 * @return
	 */
	@RequestMapping(value = "/{sid}", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
	@ResponseBody
	public ResultModel<String> updateSchool(@PathVariable("sid") String sid,@RequestBody SchoolEntity schoolEntity) {
		if(StringUtils.isEmpty(sid) || schoolEntity == null || StringUtils.isEmpty(schoolEntity.getSid())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,请求参数失败", null);
		}
		return schoolService.updateSchool(sid, schoolEntity);
	}
	
	/**
	 * 查询
	 * @param dr	删除标志
	 * @return
	 */
	@RequestMapping(value = "/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<List<SchoolEntity>> querySchoolsByDr(@PathVariable("dr") Integer dr) {
		if(dr == null) {
			return new ResultModel<List<SchoolEntity>>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		List<SchoolEntity> schools = schoolService.querySchoolsByDr(dr);
		if(Utils.isEmptyCollection(schools)) {
			return new ResultModel<List<SchoolEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<SchoolEntity>>(ResultModel.SUCCESS, "查询成功", schools);
	}
	
	/**
	 * 查询
	 * @param sid
	 * @param dr	删除标志
	 * @return
	 */
	@RequestMapping(value = "/sid/{sid}/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> querySchoolBySidAndDr(@PathVariable("sid") String sid,@PathVariable("dr") Integer dr) {
		if(StringUtils.isEmpty(sid) || StringUtils.isEmpty(dr)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		SchoolEntity school = schoolService.querySchoolSidAndDr(sid, dr);
		if(school == null) {
			return new ResultModel<List<SchoolEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<SchoolEntity>(ResultModel.SUCCESS, "查询成功", school);
	}
	
	/**
	 * 分页查询
	 * @param form	请求表单
	 * @return
	 */
	@RequestMapping(value = "/selectPageSchool", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> selectPageSchool(@RequestBody SchoolSearchForm form) {
		if(form == null || StringUtils.isEmpty(form.getDr())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		PageInfo<SchoolEntity> page = schoolService.querySchoolsBySomeFields(form.getSname(), form.getDr(), form.getPageNo(), form.getPageSize());
		return new ResultModel<PageInfo<SchoolEntity>>(ResultModel.SUCCESS, "查询成功", page);
	}
	
	/**
	 * 重置梦想中心主任
	 * @param sid
	 * @param uid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "resetDirector", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> resetDirector(String sid,String uid,HttpServletRequest request) {
		try {
			return schoolService.resetDirector(sid, uid, request);
		} catch (ADreamServiceException e) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, e.getMessage(), null);
		}
	}
	
	/**
	 * 查询当前登录的学校账号信息
	 * @param state	认证状态
	 * @param dr	删除标志
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryCurSchoolInfo", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryCurSchoolInfo(int state, int dr, HttpServletRequest request) {
		String suid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(suid)) {
			logger.warn("查询失败,suid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "当前登录信息无效,请重新登录", null);
		}
		return schoolService.querySchoolInfo(suid, state, dr);
	}
	
	/**
	 * 查询当前登录的学校账号的学校梦想教师
	 * @param state
	 * @param dr
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryCurSchoolTeachers", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryCurSchoolTeachers(int state, int dr, HttpServletRequest request) {
		String suid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(suid)) {
			logger.warn("查询失败,suid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "当前登录信息无效,请重新登录", null);
		}
		return schoolService.querySchoolTeachers(suid, state, dr);
	}
	
	/**
	 * 重新绑定当前登录的学校账号的学校梦想中心主任
	 * @param state
	 * @param dr
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "reBindCurDirector", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> reBindCurMasterAndDirector(@RequestBody ReBindDirectorForm form, HttpServletRequest request) {
		String suid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(suid)) {
			logger.warn("查询失败,suid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "当前登录信息无效,请重新登录", null);
		}
		if(StringUtils.isEmpty(form.getSid())) {
			logger.warn("查询失败,sid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "重新绑定失败,参数有误", null);
		}
		
		if(StringUtils.isEmpty(form.getDuid())) {
			logger.warn("duid为空,重新绑定失败:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "重新绑定失败,参数有误", null);
		}
		return schoolService.reBindDirector(suid, form.getSid(), form.getDuid(), form.getState(), form.getDr());
	}
}